package com.yonyou.brigade.third_parties.meiya.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yonyou.brigade.basic_init.enums.ChannelEnum;
import com.yonyou.brigade.basic_init.model.BriChannel;
import com.yonyou.brigade.basic_init.service.IBriChannelService;
import com.yonyou.brigade.third_parties.meiya.model.MeiyaConfig;
import com.yonyou.brigade.third_parties.meiya.service.IMeiyaPublicService;
import com.yonyou.ybzboot.starter.common.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * 美亚商旅公共服务 controller
 *
 * @author kangfw5
 * @since 2019-06-12
 */
@RestController
@RequestMapping("/meiya")
public class MeiyaPublicController {
    @Autowired
    private IMeiyaPublicService meiyaPublicService;
    @Autowired
    private IBriChannelService briChannelService;



    @Value("${meiya.host.pc}")
    private String pcHost;

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(path = "/getPcToken", method = RequestMethod.GET)
    @ResponseBody
    public String getPcToken(String tenant, String org) {
        BriChannel briChannel = briChannelService.getEnableInfoByTenantOrg(tenant, org, ChannelEnum.MEIYA.getName());
        MeiyaConfig meiyaConfig = JSON.parseObject(briChannel.getConfig(), MeiyaConfig.class);
        String token = meiyaPublicService.getToken(meiyaConfig);
        return token;
    }

    @RequestMapping("/apply")
    @ResponseBody
    public Object apply(@RequestBody JSONObject param){
        if (!"yonyou@998".equals(param.getString("key"))){
            throw new BusinessException("非法调用");
        }
        String tenant = param.getString("tenant");
        String orgpk = param.getString("org");
        JSONObject applyOrderJson = param.getJSONObject("apply");
        BriChannel channel = briChannelService.getEnableInfoByTenantOrg(tenant, orgpk, ChannelEnum.MEIYA.getName());
        return meiyaPublicService.pushApplyOrder(channel, tenant, orgpk, applyOrderJson);
    }


    @RequestMapping("/canncel")
    @ResponseBody
    public Object cannecl(@RequestBody JSONObject param){
        if (!"yonyou@998".equals(param.getString("key"))){
            throw new BusinessException("非法调用");
        }
        String stApplyOrderNo = param.getString("applyNo");
        String tenant = param.getString("tenant");
        String org = param.getString("org");
        BriChannel briChannel = briChannelService.getEnableInfoByTenantOrg(tenant, org, ChannelEnum.MEIYA.getName());
        MeiyaConfig meiyaConfig = JSON.parseObject(briChannel.getConfig(), MeiyaConfig.class);
        String sessionId = meiyaPublicService.getToken(meiyaConfig);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        JSONObject requestData = new JSONObject();
        requestData.put("sessionId", sessionId);
        requestData.put("stApplyOrderNo", stApplyOrderNo);
        requestData.put("status", 8);
        HttpEntity<String> entity = new HttpEntity<>(JSON.toJSONString(requestData), headers);
        String ssoResult = restTemplate.postForObject("http://api.shinetour.com" + "/API.svc/ChangeBusinessApplicationStatus", entity, String.class);
        return ssoResult;
    }


}
